﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using ECI.CommonLibrary;
using System.Management;
using ECI.BaseLanguage;

namespace ECI.DynamicData
{
    public class Authority
    {
        private string key = "";
        private string move = "";
        private static int count = 50;
        private static int times = 10;
        private static string pass = "no";

        public static void ReLoad()
        {
            pass = "no";
            Count();
        }

        private static void Count()
        {
            count = 1000;
        }

        public Authority()
        {
            if (pass == "pass") return;

            string currentValue = ConfigurationSettings.AppSettings["ECIKey"];
            string strTime = ConfigurationSettings.AppSettings["ECITryTimes"];
            if (currentValue == null) currentValue = "NULL-NULL-NULL";
            currentValue += "-A-A-A";
            string[] array = currentValue.Split('-');
            this.key = array[0];
            this.move = array[1];
            currentValue = array[2];
            if (this.key == null) this.key = "NULL";
            if (this.move == null) this.move = "N";
            if (strTime == null) strTime = times.ToString();
            times = strTime.ToIntIgnoreError();
            if (times > 50) times = 10;
            key = GetCPU();
            string value = GetMD5(key, move);
            this.CheckAuthority(value, currentValue);
        }

        private void CheckAuthority(string value, string currentValue)
        {
            count++;
            value = GetMD5(value, move);
            if (value.ToUpper() != currentValue.ToUpper())
            {
                if (count >= times)
                {
                    count = 0;
                    //PageHelper.CurrentPage.Response.Write("您目前使用的是未注册的试用版开发版组件(仅供测试使用)<br>");
                    //PageHelper.CurrentPage.Response.Write("试用版组件设定执行" + times + "次会出现本页面提示<br>");
                    //PageHelper.CurrentPage.Response.Write("您可以继续试用<br>");
                    //PageHelper.CurrentPage.Response.Write("您或者可以将下面的机器内码发送给软件提供商进行软件产品的注册<br>");

                    PageHelper.CurrentPage.Response.Write(Lang.A00048.Read());
                    PageHelper.CurrentPage.Response.Write(Lang.A00049.Read() + times + Lang.A00050.Read());
                    PageHelper.CurrentPage.Response.Write(Lang.A00051.Read());
                    PageHelper.CurrentPage.Response.Write(Lang.A00052.Read());
                    PageHelper.CurrentPage.Response.Write("<b>" + GetCPU() + "<b>");
                    PageHelper.CurrentPage.Response.End();
                }
            }
            else
            {
                pass = "pass";
            }
        }

        private string GetCPU()
        {
            string cpuInfo = "";//cpu序列号
            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }

            return cpuInfo;
        }

        private string GetMD5(string sDataIn, string move)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider
               md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytValue, bytHash; bytValue = System.Text.Encoding.UTF8.GetBytes(move + sDataIn);
            bytHash = md5.ComputeHash(bytValue); md5.Clear(); string sTemp = "";
            for (int i = 0; i < bytHash.Length; i++)
            { sTemp += bytHash[i].ToString("x").PadLeft(2, '0'); } return sTemp;
        }
    }
}
